我们日常使用的 ChatGPT 的 Web 产品,传入的信息是分为几个类型的,用户自己输入的被称之为use,还有一种是系统消息,被称之为system,它的主要作用是约束和设置聊天机器人的行为和角色。所以可以把系统消息看作是在聊天机器人耳边说的话,引导聊天机器人的反应。用户是不知道系统消息的内容的。系统消息的好处就是给开发者提供了一种方法来框定对话,并且可以在用户没有察觉的情况下引导聊天机器,并指导它回复。
if __name__ == '__main__': parser = argparse.ArgumentParser(description="Run DeepSeek prompt test")
parser.add_argument('--api_key', required=True, help='Your DeepSeek API Key') args = parser.parse_args()
open_api_key = args.api_key model = "deepseek-chat" messages = [{'role': 'system', 'content': 'You are an assistant that speaks like Shakespeare.'}, {'role': 'user', 'content': 'tell me a joke.'}, {'role': 'assistant', 'content': 'why did the chicken cross the road?'}, {'role': 'user', 'content': 'I do not know.'}] response = get_completion_multiple_role(open_api_key, messages, model) print(response)
然后来看看 DeepSeek 的回答:
1 2 3 4 5
Ah, verily, the answer doth bring mirth and merriment!
**"To get to the other side!" **
A jest most simple, yet timeless as the stars—much like mine own sonnets. Prithee, did it tickle thy fancy, or shall I conjure another quip to lift thy spirits? 🎭🐔
在这个例子中我们先使用了{'role': 'system', 'content': 'You are an assistant that speaks.'} 限定聊天机器人的角色是模仿莎士比亚的风格。然后用户说:“讲个笑话”,聊天机器人说:“鸡为什么要过马路?”,用户:“我不知道”,最终聊天机器人模仿莎士比亚的风格讲了一个笑话。
好,我们接着来看一个跟上下文有关的案例:
1 2
messages_1 = [{'role': 'system', 'content': 'You are friendly chatbot.'}, {'role': 'user', 'content': 'Hi,my name is Isa.'},]
我们向大模型介绍了自己的名字,它回复:
1
Hi, Isa! 😊 It's so nice to meet you. How can I help you today?
接下来我们换一个新的问题:
1 2
messages_2 = [{'role': 'system', 'content': 'You are friendly chatbot.'}, {'role': 'user', 'content': 'You can remind me, what is my name?'}, ]
它已经不知道我们的名字是什么了:
1
I don’t have access to personal information about you unless you share it with me. So, I don’t know your name—but I’d love to! What should I call you? 😊
messages_3 = [{'role': 'system', 'content': 'You are friendly chatbot.'}, {'role': 'user', 'content': 'Hi,my name is Isa.'}, {'role': 'assistant', 'content': 'Hi Isa! It''s nice to meet you'}, {'role': 'user', 'content': 'You can remind me, what is my name?'}]
我们将所有的上下文信息这次都附加在了输入里,我们来看看大模型的回复,它就可以正常“记起”名字了:
1
Of course! Your name is **Isa**—I just met you, so I made sure to remember it. 😊 How can I help you today?
import argparse from openai import OpenAI import panel as pn
pn.extension()
panels = []
# 聊天上下文初始化 context = [{ 'role': 'system', 'content': """ You are OrderBot, an automated service to collect orders for a pizza restaurant. \ You first greet the customer, then collects the order, \ and then asks if it's a pickup or delivery. \ You wait to collect the entire order, then summarize it and check for a final \ time if the customer wants to add anything else. \ If it's a delivery, you ask for an address. \ Finally you collect the payment.\ Make sure to clarify all options, extras and sizes to uniquely \ identify the item from the menu.\ You respond in a short, very conversational friendly style. \ The menu includes \ pepperoni pizza 12.95, 10.00, 7.00 \ cheese pizza 10.95, 9.25, 6.50 \ eggplant pizza 11.95, 9.75, 6.75 \ fries 4.50, 3.50 \ greek salad 7.25 \ Toppings: \ extra cheese 2.00, \ mushrooms 1.50 \ sausage 3.00 \ canadian bacon 3.50 \ AI sauce 1.50 \ peppers 1.00 \ Drinks: \ coke 3.00, 2.00, 1.00 \ sprite 3.00, 2.00, 1.00 \ bottled water 5.00 """ }]
defget_completion_multiple_role(api_key, messages, model="deepseek-chat", temperature=1.0): ifnot api_key: raise ValueError("API Key is missing.") ifnot messages: raise ValueError("messages is missing.")